<?php

namespace App\Http\Controllers\Api\v1;

use App\Models\Menu;
use App\Transformers\MenuTransformer;
use Illuminate\Http\Request;

class MenuController extends BaseController
{

    /**
     * Instanceof menu
     *
     * @var Menu
     */
    private $model;

    /**
     * Instanceof menuTransformer
     *
     * @var MenuTransformer
     */
    private $transformer;


    /**
     * menuController constructor.
     * @param Menu $model
     * @param MenuTransformer $transformer
     */
    public function __construct(Menu $model, MenuTransformer $transformer)
    {
        $this->model = $model;
        $this->transformer = $transformer;
        $this->middleware('jwt.auth', ['except' => ['index', 'show']]);
        $this->middleware('jwt.refresh', ['except' => ['index', 'show']]);
    }

    /**
     * @param Request $request
     * @return \Dingo\Api\Http\Response
     */
    public function index(Request $request)
    {
        $originPerPage = $request->get('perPage', 15);
        if ((int)$originPerPage === -1) {
            $perPage = 9999;
        } else {
            $perPage = (int)$originPerPage;
        }
        $sortBy = $request->get('sortBy', 'id,desc');
        $search = $request->get('search');
        $appends = [];
        $appends = !$perPage ?? array_add($appends, 'perPage', $originPerPage);
        $appends = !$sortBy ?? array_add($appends, 'sortBy', $sortBy);
        $appends = !$search ?? array_add($appends, 'search', $search);

        $menus = $this->model->filter($request->all())->paginate($perPage);
        $menus = $menus->appends($appends);
        return $this->response->paginator($menus, $this->transformer);
    }

    /**
     * @param $id
     * @return \Dingo\Api\Http\Response|mixed
     */
    public function show($id)
    {
        $menu = $this->model->query()->find($id);
        if (!$menu) {
            return $this->response->errorNotFound();
        }
        return $this->response->item($menu, $this->transformer);
    }

    /**
     * @param Request $request
     * @return \Dingo\Api\Http\Response|mixed
     * @throws \Throwable
     */
    public function store(Request $request)
    {
        $this->validateField($request, $this->createRules());
        $menu = $this->model->query()->create($request->all());
        if (!$menu) {
            return $this->response->errorInternal('Error occurred on creating menu');
        }
        return $this->response->item($menu, $this->transformer);
    }

    /**
     * @param Request $request
     * @param $id
     * @return \Dingo\Api\Http\Response|mixed
     * @throws \Throwable
     */
    public function update(Request $request, $id)
    {
        $menu = $this->model->query()->find($id);
        if (!$menu) {
            return $this->response->errorNotFound();
        }
        $this->validateField($request, $this->updateRules($menu));
        $saved = $menu->update($request->only($this->fields()));
        if (!$saved) {
            return $this->response->errorInternal('Error occurred on updating menu');
        }
        return $this->response->item($menu, $this->transformer);
    }

    /**
     * @param $id
     * @return \Dingo\Api\Http\Response|mixed
     * @throws \Exception
     */
    public function destroy($id)
    {
        $ids = explode(',', $id);
        $menu = $this->model->query()->whereIn('id', $ids);
        if (!$menu) {
            return $this->response->errorNotFound();
        }
        $deleted = $menu->delete();
        if (!$deleted) {
            return $this->response->errorInternal('Error occurred on updating menu');
        }
        return $this->response->noContent();
    }

    /**
     * @return array
     */
    public function createRules(): array
    {
        return [
            'menu_type' => 'required',
            'name' => 'required|unique:menus,name',
//            'direct' => 'required',
        ];
    }

    /**
     * @param Menu $model
     * @return array
     */
    public function updateRules($model): array
    {
        return [
            'menu_type' => 'required',
            'name' => 'required|unique:menus,name,' . $model->id,
//            'direct' => 'required',
        ];
    }

    public function fields()
    {
        return ['menu_type', 'menu_id', 'name', 'direct', 'order', 'status'];
    }


}